<?php
/**
 * Created by PhpStorm.
 * User: acker
 * Date: 21-06-08
 * Time: 下午6:25
 */

namespace App\Models\yb;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Model;
use Encore\Admin\Traits\DefaultDatetimeFormat;


class SettingModel extends Model
{
    use DefaultDatetimeFormat;

    use SoftDeletes;

    /**
     *
     *计量单位
     * @var string
     */
    protected $table = 'yb_setting';

    /**
     * 主键
     *
     * @var string
     */
    protected $primaryKey = 'id';



    /**
     * 该模型是否被自动维护时间戳
     *
     * @var bool
     */
    public $timestamps = true;


    public static function InverseIntegral($user_id,$amount){
        if($amount>0){
            $setting_data = Self::select(['consumption_inverse_price','consumption_inverse_points'])->first();

            if(!empty($setting_data['consumption_inverse_price'])){
                $points = floor($amount/$setting_data['consumption_inverse_price'])*$setting_data['consumption_inverse_points'];

                $user_data = UserModel::where(['id'=>$user_id])->first();
                UserModel::where(['id'=>$user_id])->update([
                    'surplus_points' => $user_data['surplus_points']+$points,
                    'earn_points' => $user_data['earn_points']+$points,
                ]);
                UserWalletRecordModel::insert([
                    'user_id'=>$user_data['id'],
                    'openid'=>$user_data['openid'],
                    'phone'=>$user_data['phone'],
                    'avatar'=>$user_data['avatar'],
                    'nickname'=>$user_data['nickname'],
                    'amount'=>$points,
                    'status'=>1,
                    'remark'=>'消费反积分',
                    'created_at'=>date('Y-m-d H:i:s',time())
                ]);
            }
        }
        return true;

    }

}
